<?php

class Friend extends Model {

    public $from;
    public $to;

    public function __construct() {
        parent::__construct();
        //$this->_initialize($params);
    }

    public function add($id, $idFriend) {
        if (empty($id) || empty($idFriend)) {
            return false;
        }

        $sql = sprintf("INSERT INTO `l4_friend_request`(`from_user_id`,`to_user_id`, `time`)
				VALUES(%s, %s, NOW())", $id, $idFriend);

        $this->db->queryshort($sql, DB_NAME);
        $row_affected = $this->db->affectedRow();

        if ($row_affected > 0) {
            //Send email to request.
            $this->_sendRequest($id,$idFriend);
            return true;
        } else {
            return false;
        }
    }

    public function unfriend($id, $idFriend) {
        if (empty($id) || empty($idFriend)) {
            return false;
        }

        $idFriend = mysql_real_escape_string($idFriend);
        $sql = "DELETE FROM `l4_friend_relation` WHERE `user_id_1`=%s AND `user_id_2`=%s OR `user_id_1`=%s AND `user_id_2`=%s ";
        $sql = sprintf($sql, $id, $idFriend, $idFriend, $id);
        $this->db->queryshort($sql, DB_NAME);
        $affectedRow = $this->db->affectedRow();

        if ($affectedRow > 0) {
            return true;
        } else {
            return false;
        }
        return false;
    }

    public function accept($id, $idFriend) {

        if (empty($id) || empty($idFriend)) {
            return false;
        }

        $sql = sprintf("INSERT INTO `l4_friend_relation`(`user_id_1`,`user_id_2`)
				VALUES(%s, %s)", $id, $idFriend);

        $this->db->startTrans();
        $this->db->queryshort($sql, DB_NAME);

        $sql = sprintf("INSERT INTO `l4_friend_relation`(`user_id_1`,`user_id_2`)
				VALUES(%s, %s)", $idFriend, $id);

        $this->db->queryshort($sql, DB_NAME);
        //Detele from friend request table...
        $sql = "DELETE FROM `l4_friend_request` WHERE `from_user_id`=%s AND `to_user_id`=%s";

        $sql = sprintf($sql, $idFriend, $id);
        $this->db->queryshort($sql, DB_NAME);

        $row_affected = $this->db->affectedRow();

        if ($row_affected > 0) {
            $this->db->commit();
            return true;
        } else {
            $this->db->rollback();
            return false;
        }
    }

    public function deny($id, $idFriend) {
        if (empty($id) || empty($idFriend)) {
            return false;
        }
        $idFriend = mysql_real_escape_string($idFriend);
        $sql = "DELETE FROM `l4_friend_request` WHERE `from_user_id`=%s AND `to_user_id`=%s";
        $sql = sprintf($sql, $idFriend, $id);
        $this->db->queryshort($sql, DB_NAME);
        $affectedRow = $this->db->affectedRow();

        if ($affectedRow > 0) {
            return true;
        } else {
            return false;
        }
        return false;
    }

    public function gets($idLog, $idView) {
        if (empty($idLog) || empty($idView)) {
            return FALSE;
        }

        $sql = sprintf("SELECT * ,
                        (SELECT COUNT(*) FROM `l4_friend_relation` c WHERE c.`user_id_1`=%s AND c.`user_id_2`=a.`user_id_2`)
                                AS `friended` ,
                        (SELECT COUNT(*) FROM `l4_friend_request` d WHERE d.`from_user_id`=%s AND d.`to_user_id`=a.`user_id_2`)
                                AS `requested`
                        FROM `l4_friend_relation` a
                                INNER JOIN `l4_user` b ON a.user_id_2 = b.id
                        WHERE user_id_1=%s", $idLog, $idLog, $idView);


        $this->db->queryshort($sql, DB_NAME);
        $numRow = $this->db->numRow();

        if ($numRow > 0) {
            return $this->db->getArrayObject();
        }
        return NULL;
    }

    public function getRequest($id) {
        if (empty($id)) {
            return FALSE;
        }
        $sql = "DELETE FROM `l4_friend_request` WHERE DATEDIFF(NOW(), `time`) > 7";
        $this->db->queryshort($sql, DB_NAME);

        $sql = sprintf("SELECT * FROM `l4_friend_request` a INNER JOIN `l4_user` b ON a.from_user_id = b.id WHERE `to_user_id`=%s", $id);
        $this->db->queryshort($sql, DB_NAME);
        $numRow = $this->db->numRow();

        if ($numRow > 0) {
            return $this->db->getArrayObject();
        }
        return NULL;
    }

    public function getFriendRelation($id, $idFriend) {
        if (empty($id) || empty($idFriend)) {
            return FALSE;
        }

        //User is self
        if ($id == $idFriend) {
            return -1;
        }

        //User receive request...
        $sql = sprintf("SELECT * FROM `l4_friend_request` WHERE `to_user_id`=%s AND `from_user_id`=%s", $idFriend, $id);
        $this->db->queryshort($sql, DB_NAME);
        $numRow = $this->db->numRow();

        if ($numRow > 0) {
            return FRIEND_REQUEST;
        }

        //User is friend
        $sql = sprintf("SELECT * FROM `l4_friend_relation` WHERE `user_id_1`=%s AND `user_id_2`=%s", $id, $idFriend);
        $this->db->queryshort($sql, DB_NAME);
        $numRow = $this->db->numRow();

        if ($numRow > 0) {
            return FRIEND;
        }

        //User not friend.
        return NOT_FRIEND;
    }

    private function _sendRequest($id, $idFriend) {
        $superThis =& getInstance();
        $superThis->model('user');
        $to = $superThis->user->getInfoById($idFriend)->email;
        $name = $superThis->user->getInfoById($id)->fullname;
        $subject = "Request to you.";;

        $message = sprintf("%s add you to list friend", $name);
        //var_dump($message);
        $from = "noreply@lampart.com";
        $headers = "From:" . $from;
        mail($to, $subject, $message, "From:" . $from);
    }
}
?>

